package com.cade.agriculturemall.widget.photo;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.widget.Toast;

import com.yancy.gallerypick.config.GalleryConfig;
import com.yancy.gallerypick.config.GalleryPick;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tom on 17/8/7.
 */

public class PhotoPicker implements PermissionListener {

    private static final String FileProvider = "com.eyevision.health.fileprovider";

    private static final String FilePath = "/.Eyevision/tmp/Pictures";

    /**
     * 修改配置可参考：PhotoConfig.defaultConfig().getBuilder().multiSelect(false).build()
     *
     * @param callback
     * @return
     */
    public static GalleryConfig defaultConfig(PhotoHandlerCallback callback, List<String> selectPath, boolean isMultiSelect, int maxCount,
                                              boolean isCrop, float scaleX, float scaleY,
                                              int width, int height) {
        GalleryConfig galleryConfig = new GalleryConfig.Builder()
                .imageLoader(new PhotoGlideImageLoader())    // ImageLoader 加载框架（必填）
                .iHandlerCallBack(callback)     // 监听接口（必填）
                .provider(FileProvider)   // provider (必填)
                .pathList(selectPath)                         // 记录已选的图片
//                .multiSelect(false)                      // 是否多选   默认：false
                .multiSelect(isMultiSelect, maxCount)                   // 配置是否多选的同时 配置多选数量   默认：false ， 9
                .maxSize(maxCount)                             // 配置多选时 的多选数量。    默认：9
                .crop(false)                             // 快捷开启裁剪功能，仅当单选 或直接开启相机时有效
                .crop(isCrop, scaleX, scaleY, width, height)             // 配置裁剪功能的参数，   默认裁剪比例 1:1
                .isShowCamera(true)                     // 是否现实相机按钮  默认：false
                .filePath(FilePath)          // 图片存放路径
                .build();


        return galleryConfig;
    }

    private Activity mActivity;

    private PhotoHandlerCallback mCallback;

    private boolean mIsMulti = false;

    private int mMaxCount = 1;

    private List<String> mSelectPath;

    private boolean isCrop = false;

    private float mScaleX = 1;

    private float mScaleY = 1;

    private int mWidth = 1;

    private int mHeight = 1;

    public PhotoPicker(Activity activity) {
        mActivity = activity;
    }


    /**
     * 选择一张图片
     *
     * @param callback 回调接口
     */
    public void openSinglePicker(PhotoHandlerCallback callback) {
        mCallback = callback;
        mMaxCount = 1;
        mIsMulti = false;
        isCrop = false;
        mSelectPath = null;
        mScaleX = 1;
        mScaleY = 1;
        mWidth = 1;
        mHeight = 1;
        requestPermission();
    }

    /**
     * 选择一张图片并缩放
     *
     * @param callback 回调接口
     * @param scaleX   x轴缩放比例
     * @param scaleY   y轴缩放比例
     * @param width    宽
     * @param height   高
     */
    public void openSinglePickerWithCrop(PhotoHandlerCallback callback, float scaleX, float scaleY, int width, int height) {
        mCallback = callback;
        mMaxCount = 1;
        mIsMulti = false;
        isCrop = true;
        mSelectPath = null;
        mScaleX = scaleX;
        mScaleY = scaleY;
        mWidth = width;
        mHeight = height;
        requestPermission();
    }

    /**
     * 选择多张图片
     *
     * @param callback   回调接口
     * @param selectPath 已选的图片路径
     * @param maxCount   最大可选数量
     */
    public void openMultiPicker(PhotoHandlerCallback callback, List<String> selectPath, int maxCount) {
        mCallback = callback;
        mMaxCount = maxCount;
        mIsMulti = true;
        isCrop = false;
        mSelectPath = selectPath;
        mScaleX = 1;
        mScaleY = 1;
        mWidth = 1;
        mHeight = 1;
        requestPermission();
    }

    /**
     * 选择多张图片并缩放
     *
     * @param callback   回调接口
     * @param selectPath 已选的图片路径
     * @param maxCount   最大可选数量
     * @param scaleX     x轴缩放比例
     * @param scaleY     y轴缩放比例
     * @param width      宽
     * @param height     高
     */
    public void openMultiPickerWithCrop(PhotoHandlerCallback callback, List<String> selectPath, int maxCount, float scaleX, float scaleY, int width, int height) {
        mCallback = callback;
        mMaxCount = maxCount;
        mIsMulti = true;
        isCrop = true;
        mSelectPath = selectPath;
        mScaleX = scaleX;
        mScaleY = scaleY;
        mWidth = width;
        mHeight = height;
        requestPermission();
    }

    private void requestPermission() {
        AndPermission.with(mActivity)
                .permission(Permission.CAMERA, Permission.STORAGE)
                .callback(this)
                .start();
    }

    @Override
    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
        if (mSelectPath == null) {
            mSelectPath = new ArrayList<>();
        }
        GalleryPick.getInstance()
                .setGalleryConfig(defaultConfig(mCallback, mSelectPath, mIsMulti, mMaxCount, isCrop, mScaleX, mScaleY, mWidth, mHeight))
                .open(mActivity);
    }

    @Override
    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
        Toast.makeText(mActivity, "摄像头或者存储的权限没有打开，无法选择图片。", Toast.LENGTH_SHORT).show();
    }
}
